package com.elong.android.specialhouse.ui;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.elong.android.specialhouse.R;

import java.util.ArrayList;
import java.util.List;


public class IndexDotsView extends LinearLayout {

	private int mDotCounts;
	private List<ImageView> mDotsList = new ArrayList<ImageView>();
	private Drawable mDot_n;
	private Drawable mDot_h;
	public static final double SCALE = 1.4;
	private LayoutParams mParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

	public IndexDotsView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mDot_n = context.getResources().getDrawable(R.drawable.pos_n);
		mDot_h = context.getResources().getDrawable(R.drawable.pos_h);
		int marginLeft = (int) getResources().getDimension(R.dimen.dot_margin);
		mParams.setMargins(marginLeft, 0, 0, 0);
		mParams.height = (int) (mDot_h.getMinimumHeight() / SCALE);
		mParams.weight = (int) (mDot_h.getMinimumWidth() / SCALE);
	}

	public void initViews(int dotCounts, int currentIndex) {
		init();

		mDotCounts = dotCounts;
		for (int i = 0; i < mDotCounts; i++) {
			ImageView imageView = new ImageView(getContext());
			imageView.setImageDrawable(mDot_n);
			addView(imageView, mParams);
			mDotsList.add(imageView);

		}

		setCurrent(currentIndex);
	}

	public void setCurrent(int currentIndex) {
		if (mDotsList != null && mDotsList.size() > 0) {
			for (int i = 0; i < mDotsList.size(); i++) {
				ImageView iv = mDotsList.get(i);
				if (currentIndex == i) {
					iv.setImageDrawable(mDot_h);
				} else {
					iv.setImageDrawable(mDot_n);
				}
			}
		}
	}

	public void init() {
		removeAllViews();
		mDotsList.clear();
		mDotCounts = 0;
	}
}
